package Lock;

import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;

public class BankAccont {
    private final ReentrantLock lock = new ReentrantLock();
    private Double balance;

    public BankAccont(Double balance) {
        this.balance = balance;
    }

    public static void main(String[] args) {
        BankAccont bankAccont = new BankAccont(100000.00);
        Thread thread1 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("要存入的金额");
            double v = scanner.nextDouble();
            bankAccont.putmoney(v);
        });
        Thread thread2 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("要取的金额");
            double v1 = scanner.nextDouble();
            bankAccont.getmoney(v1);
        });
        thread1.start();
        thread2.start();

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("余额=" + bankAccont.Balance());
    }

    public void putmoney(Double money) {
        lock.lock();
        try {
            balance += money;
            System.out.println("你存了" + money);
            System.out.println("你的余额" + balance);
        } finally {
            lock.unlock();
        }

    }

    public void getmoney(Double money) {
        lock.lock();
        try {
            if (balance >= money) {
                balance -= money;
                System.out.println("你取出了" + money);
                System.out.println("你的余额" + balance);
            } else {
                System.out.println("不准取快存钱你都没钱了");
            }

        } finally {
            lock.unlock();
        }
    }

    public Double Balance() {
        if (balance < 100000.00) {
            System.out.println("余额都没有10000了快挣钱去");
        }
        return balance;
    }
}
